package com.EJ.ejcompanyweb.HttpURLC;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.EJ.ejcompanyweb.R;
import com.EJ.ejcompanyweb.adapter.MyAdapter;
import com.EJ.ejcompanyweb.entity.DateEntity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class JsoupAsyncSearchData extends AsyncTask<String,Void, ArrayList<DateEntity>> {
    private String TAG=this.getClass().getSimpleName();
    private ArrayList<DateEntity> DateEntityS=new ArrayList<DateEntity>() ;
    private RecyclerView UiShoplist;
    private Context context;
    private ArrayList<DateEntity> UIdata;
    private MyAdapter UIadapter;
    public JsoupAsyncSearchData(Context context, RecyclerView uiShoplist,MyAdapter UIadapter,ArrayList<DateEntity> UIdata) {
        this.UiShoplist = uiShoplist;
        this.context=context;
        this.UIadapter=UIadapter;
        this.UIdata=UIdata;
    }

    @Override
    protected ArrayList<DateEntity> doInBackground(String... strings) {
        GetArmariumData(strings[0]);
        return DateEntityS;
    }

    @Override
    protected void onPostExecute(ArrayList<DateEntity> dateEntities) {
        super.onPostExecute(dateEntities);
        //获取到搜索到的数据源
        if (!dateEntities.isEmpty()){
            if (!UIdata.isEmpty()){
                UIdata.clear();
            }
            UIdata.addAll(dateEntities);
            UIadapter.notifyDataSetChanged();
            UiShoplist.invalidate();
///
//            mDeviceList.add(device);
            //数据源发送改变
 //           adapter.clear;
//            adapter.addAll();
 //           adapter.notifyDataSetChanged();
  //          notifyDataSetInvalidated()：//更换数据yuan
     //       当adapter中的数据集要整个更换时，你需要调用此方法来重绘控件。
        }else {
            Toast.makeText(context,"暂无数据",Toast.LENGTH_SHORT).show();
        }
    }

    private void GetArmariumData(String url){
        Document doc= null;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(doc==null){
            return;
        }
        ArrayList<DateEntity> al2 = new ArrayList<DateEntity>();
        //product-wrapper  所有搜索到的内容
        Elements divs=doc.select("div.row");
        //  product-thumb 符合商品的具体子类
        Elements thumbs=divs.select("div.product-thumb");
        for (Element thumb : thumbs) {

            //价格标签
            Elements price = thumb.select("p.price");
            // Log.e(TAG, price.text());

            Elements images = thumb.select("div.image");
            for (Element image : images) {
                //商品连接
                String url1 = image.select("a").attr("href");

                Elements img = image.select("img");
                //商品图像
                String BitmapUrl = img.attr("src");
                //商品名称
                String titel = img.attr("title");
                //商品介绍
                String alt=img.attr("alt");
                //Log.e(TAG, url1);
                al2.add(new DateEntity(titel,alt,url1,BitmapUrl,price.text()));

            }
        }
        for (DateEntity i:al2){
            Log.e(TAG,i.toString());
        }
        DateEntityS=al2;
    }
}
